package com.moorror.codegenvel.common.table;

import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.util.ui.ListTableModel;

import java.util.ArrayList;

/**
 * @Author jiyanghuang
 * @Date 2025/8/26 08:21
 */
public class TableAddNewRowAction<T extends TreeLineObject> implements AnActionButtonRunnable {

    private Class<T> clazz;

    private ListTableModel<TreeLineWrap> listTableModel;

    public TableAddNewRowAction(Class<T> clazz, ListTableModel<TreeLineWrap> listTableModel) {
        this.clazz = clazz;
        this.listTableModel = listTableModel;
    }

    /**
     * 核心思路是获取原来的对象
     * 添加一列后
     * 再把对象填充
     * 因此更新后最好是获取ListTableModel对象
     * @param anActionButton
     */
    @Override
    public void run(AnActionButton anActionButton) {
        ArrayList<TreeLineWrap> newList = new ArrayList(listTableModel.getItems());
        TreeLineWrap newLine = TreeLineWrap.createRowValue(this.clazz);
        if(null != newLine){
            newList.add(newLine);
            listTableModel.setItems(newList);
        }

    }
}
